﻿Imports System.Text
Imports System.Security.Cryptography
Public Class Login
    Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
        Try
            Dim Ue As New UnicodeEncoding()
            'Retrieve a byte array based on the source text
            Dim ByteSourceText() As Byte = Ue.GetBytes(PasswordTextBox.Text)
            'Instantiate an MD5 Provider object
            Dim Md5 As New MD5CryptoServiceProvider()
            'Compute the hash value from the source
            Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
            'And convert it to String format for return
            encryptedTextBox.Text = Convert.ToBase64String(ByteHash)
            Dim FTPUser As String = "Accounts@heroica.freehostingcloud.com"
            Dim FTPPass As String = "isaacval"
            Dim FTP As String = "ftp://heroica.freehostingcloud.com/Users/"
            Dim FTPFile As String = UsernameTextBox.Text + ".xml"
            Dim XMLLocation As String = My.Computer.FileSystem.SpecialDirectories.Temp + "\heroica\" + UsernameTextBox.Text + ".xml"
            If UsernameTextBox.Text = "" Or UsernameTextBox.Text = " " Or PasswordTextBox.Text = "" Or PasswordTextBox.Text = " " Then
                MsgBox("Please enter your Username and Password")
            Else
                If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.Temp + "\heroica\" + UsernameTextBox.Text + ".xml") Then
                    Kill(My.Computer.FileSystem.SpecialDirectories.Temp + "\heroica\" + UsernameTextBox.Text + ".xml")
                End If
                Try
                    My.Computer.Network.DownloadFile(FTP + FTPFile, XMLLocation, FTPUser, FTPPass)
                Catch ex As Exception
                    MsgBox(ex.ToString & " You probably didn't make an account :/")
                End Try
                Dim playerXMLSaveFile As XDocument = XDocument.Load(My.Computer.FileSystem.SpecialDirectories.Temp + "\heroica\" + UsernameTextBox.Text + ".xml")
                Dim desc = playerXMLSaveFile.Descendants("Info").FirstOrDefault()
                Dim User As String = desc...<name>.Value
                Dim Pass As String = desc...<pass>.Value
                If UsernameTextBox.Text = User And encryptedTextBox.Text = Pass Then
                    My.Settings.Username = UsernameTextBox.Text
                    My.Settings.Password = PasswordTextBox.Text
                    My.Settings.Save()
                    Me.Hide()
                    Game.Show()
                Else
                    MsgBox("Username or Password is incorrect")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub RegisterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisterButton.Click
        Me.Hide()
        CreateUser.Show()
    End Sub
    Private Sub NewsLoader_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles NewsLoader.DocumentCompleted
        NewsLoader.Show()
        LoadingLabel.Dispose()
    End Sub
    Private Sub Login_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        End
    End Sub
    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UsernameTextBox.Text = My.Settings.Username
        PasswordTextBox.Text = My.Settings.Password
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Settings.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox("Not done yet...")
    End Sub
End Class
